package com.pay.cloud.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.cloud.KeyInfo;
import com.pay.cloud.R;
import com.pay.cloud.ScanListener;
import com.pay.cloud.fragment.PwdValidateFragment;
import com.pay.cloud.fragment.RefundIngFragment;
import com.pay.cloud.fragment.RefundScanFragment;
import com.pay.cloud.fragment.StatisticsMenuFragment;
import com.pay.cloud.fragment.StatisticsOverviewFragment;
import com.pay.cloud.fragment.TipVerticalFragment;

import static com.pay.cloud.KeyInfo.KEY_CANCEL;
import static com.pay.cloud.KeyInfo.KEY_ENTER;
import static com.pay.cloud.KeyInfo.KEY_NUM_1;
import static com.pay.cloud.KeyInfo.KEY_NUM_2;

public class StatisticsActivity extends AbstractActivity implements ScanListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics);
        setMainFragment(new StatisticsMenuFragment());
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (mainFragment instanceof StatisticsMenuFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            } else if (keyInfo == KEY_NUM_1) {
                setMainFragment(new StatisticsOverviewFragment());
            } else if (keyInfo == KEY_NUM_2) {
                showRefundPwdInput();
            }
        } else if (mainFragment instanceof StatisticsOverviewFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                setMainFragment(new StatisticsMenuFragment());
            }
        } else if (mainFragment instanceof PwdValidateFragment) {
            PwdValidateFragment fragment = ((PwdValidateFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            if (keyInfo == KEY_CANCEL) {
                setMainFragment(new StatisticsMenuFragment());
            }
        } else if (mainFragment instanceof RefundScanFragment) {
            if (keyInfo == KEY_CANCEL) {
                setMainFragment(new StatisticsMenuFragment());
            }
        } else if (mainFragment instanceof TipVerticalFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                setMainFragment(new StatisticsMenuFragment());
            }
        }
        return false;
    }

    private void showRefundPwdInput() {
        PwdValidateFragment fragment = new PwdValidateFragment();
        fragment.setOnPwdValidateListener(() -> showRefundScan());
        setMainFragment(fragment);
    }

    private void showRefundScan() {
        RefundScanFragment fragment = new RefundScanFragment();
        fragment.setListener(this);
        setMainFragment(fragment);
    }

    @Override
    public boolean onScan(String refundNo) {
        setMainFragment(new RefundIngFragment(refundNo));
        return true;
    }
}
